﻿<%@ Page Title="" Language="C#"  AutoEventWireup="true" CodeFile="r_desenvolvimento_foruns_por_situacao.aspx.cs" Inherits="relatorios_r_desenvolvimento_foruns_por_situacao" %>
<%
    string Permissao_RelatoriosGerais = "" + this.Session["Permissao_RelatoriosGerais"];
    if (Permissao_RelatoriosGerais != "1")
    {
        this.Response.Redirect("../login.aspx");
    }
%>
<div style="background-color: White;">
    <%
        bool hasResults = false;
        DateTime now = DateTime.Now;
        string month = this.Request["month"];
        string year = this.Request["year"];            
        int iMonth = -1;
        int iYear = -1;            
        int maxDay = 31;
            
        // Month
        if (string.IsNullOrEmpty(month) == true)
        {
            iMonth = now.Month;
        }
        else
        {
            try
            {
                iMonth = int.Parse(month);
            }
            catch { iMonth = now.Month; }
        }
        // Year            
        if (string.IsNullOrEmpty(year) == true)
        {
            iYear = now.Year;
        }
        else
        {
            try
            {
                iYear = int.Parse(year);
            }
            catch { iYear = now.Year; }                
        }

        maxDay = DateTime.DaysInMonth(iYear, iMonth);            
        //DateTime targetDate = DateTime.Now;     
    %>
    <center>
        <form>
            <table>
                <tr>
                    <td>Mês</td>
                    <td>Ano</td>
                    <td></td>
                </tr>

                <tr>
                    <td>
                        <input type="text" id="month" name="month" value="<%= iMonth %>" />
                    </td>
                    <td><input type="text" id="year" name="year" value="<%= iYear %>" /></td>
                    <td><input type="submit" value="ok" /></td>
                </tr>
            </table>
        </form>
    </center>

    <%
        string testado = "0";
        string pendente = "0";
        string autorizado = "0";
        string finalizado = "0";

        hasResults = false;
        PL.DataBase.DAO.Instance.Setup(PL.DataBase.DatabaseTypes.SqlServer, PL.General.Instance.ForumConnectionString);
        PL.DataBase.DAO.Instance.Connector.Open();

        DateTime targetDate = new DateTime(iYear, iMonth, 1);
        DateTime maxDate = new DateTime(iYear, iMonth, maxDay);
                
        string sql = "";
        sql += " SELECT        sit.Situacao, COUNT(sit.Situacao) AS n ";
        sql += " FROM            Testes AS t INNER JOIN ";
        sql += " SAC.dbo.main AS cli ON cli.Código = t.Cod_Cliente INNER JOIN ";
        sql += " users AS us ON us.Codigo = t.Cod_Usuario INNER JOIN ";
        sql += " tipos AS tp ON t.Tipo = tp.Codigo INNER JOIN ";
        sql += " situacoes AS sit ON sit.Codigo = t.Status INNER JOIN ";
        sql += " SAC.dbo.product AS prod ON prod.Código = t.Cod_Sistema ";
        sql += " WHERE        ";
        sql += " t.Data >= '" + targetDate.Day + "/" + targetDate.Month + "/" + targetDate.Year + "' AND  ";
        sql += " t.Data <= '" + maxDate.Day + "/" + maxDate.Month + "/" + maxDate.Year + "' ";
        sql += " AND (tp.Tipo LIKE 'Corre%') AND (sit.Situacao = 'Pendente' OR ";
        sql += " sit.Situacao = 'Autorizado' OR ";
        sql += " sit.Situacao = 'Testado' OR sit.Situacao = 'Finalizado') AND (t.Cod_Cliente <> 2) AND (t.Cod_Cliente <> 1) ";
        sql += " GROUP BY sit.Situacao ";
        var reader = PL.DataBase.DAO.Instance.Connector.QueryWithReader(sql);
        while (reader.Read() == true)
        {
            hasResults = true;
            string situacao = "" + reader["Situacao"];
            if (situacao == "Pendente")
            {
                pendente = "" + reader["n"];
            }
            if (situacao == "Autorizado")
            {
                autorizado = "" + reader["n"];
            }
            if (situacao == "Testado")
            {
                testado = "" + reader["n"];
            }
            if (situacao == "Finalizado")
            {
                finalizado = "" + reader["n"];
                
            }                                    
        }
        reader.Close();
        PL.DataBase.DAO.Instance.Connector.Close();
    %>

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", { packages: ["corechart"] });
        google.setOnLoadCallback(drawChart);
        function drawChart() {

          var data = google.visualization.arrayToDataTable([
          ['Situação', 'Foruns'],
          ['Testado', <%= testado %>],
          ['Pendente', <%= pendente %>],
          ['Autorizado', <%= autorizado %>],
          ['Finalizado', <%= finalizado %>]          
        ]);

            <%
                if (hasResults == true)
                {
            %>
                    var options = {
                        title: 'Foruns por situação'
                    };
                    var chart = new google.visualization.PieChart(document.getElementById('piechart'));
                    chart.draw(data, options);
            <%
                }
            %>
        }
    </script>
    <div id="piechart" style="width: 900px; height: 500px;">
        <center>nenhum dado encontrado</center>
    </div>
</div>